// /www/client/items.js
// provide global main to spaceport
// initial load and enterFrame game loop
//
// Harald Rudell
// David DC Collier

// get an object from the swf library
function getObj(linkage) {
    var ObjClass = getLibrary().getDefinition(linkage);
    if (!ObjClass) {
        //WTF this never errors anyway
        console.log("couldnt find linkage:" + linkage);
    } else {
        //log("loaded a:" + img + " class=" + ObjClass )
    }
    return new ObjClass();
}

// Box constructor: a Box is a moving object on the canvas

var tmpObj;

function Box(x, y, width, height, itemType, imageNumber, id) {
  if (!itemType || itemType == "player") {
      // imageNumber=1
    itemType = "pet" + imageNumber
  }

    this.fixture = new b2FixtureDef();
    this.fixture.density = 1.0;
    this.fixture.friction = 0.5;
    this.fixture.restitution = 0.5;
    this.fixture.shape = new b2PolygonShape();
    this.fixture.shape.SetAsBox(width / 2, height / 2);

    this.body = new b2BodyDef();
//				this.body.type = b2Body.b2_staticBody;
    this.body.position.x = x;
    this.body.position.y = y;

    this.view = new sp.Sprite();
    clip = getObj(itemType);
    this.view.addChild(clip);
    this.clip = clip;
    this.id = id

    /*
    try {
        clip.gotoAndPlay("ready"); //WTF this just blows up
    } catch {
        log("blow UP!")
    }
    */
    
    this.world = null;

    // needed for collisions
    this.itemType = itemType;
    data = {itemType: itemType, view: this.view, clip: this.clip};
    this.body.userData = data;

}

function addBackground(stage) {
    ////WTF this just bombs with no info
    view = new sp.Sprite()
    clip = getObj("questo")
    view.addChild(clip)
    view.scaleX = 2
    stage.addChild(view)
}



var scorebox;
var score = 0;

function addScore(stage) {
    //    var score = new Box(100, 100, 100, 100, "score");
    view = new sp.Sprite();
    clip = getObj("scorebox");
    scorebox = clip;
    scorebox.inner.text = "---";
    view.addChild(clip);
    stage.addChild(view)
}


//TODO - NO MORE GLOBALLSSS!!!
function scorePoint() {
    score += 1;
    scorebox.gotoAndPlay("boom")
    scorebox.inner.text = "[" + score + "]"
    log("score=" + score)
}
